//package com.weseal.common.interceptor;
//
//import com.weseal.query.LoginRecordQuery;
//import com.weseal.service.FunctionInfoService;
//import com.weseal.service.LoginRecordService;
//import com.weseal.service.MasterInfoService;
//import com.weseal.utils.CommonUtils;
//import com.weseal.utils.HttpUtil;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.log4j.Logger;
//import org.springframework.beans.factory.InitializingBean;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.http.Cookie;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import java.util.Set;
//import java.util.regex.Pattern;
//
///**
// * @author weseal
// * @title
// * @description
// * @usage
// * @copyright Copyright 2014  rrmj Corporation. All rights reserved.
// * @company 上海众多美网络科技有限公司
// * @create 2017/9/12
// */
//public class LoginInterceptor implements HandlerInterceptor, InitializingBean {
//
//	private static final Logger LOGGER = Logger.getLogger(LoginInterceptor.class);
//
//
//	@Autowired
//	private HttpServletRequest request;
//
//	@Autowired
//	private HttpServletResponse response;
//
//	@Autowired
//	private MasterInfoService masterInfoService;
//
//	@Autowired
//	private LoginRecordService loginRecordService;
//
//	@Autowired
//	private FunctionInfoService functionInfoService;
//
//	private String[] excludeArray;
//
//	@Value("${exclude.url}")
//	private String excludeUrl;
//
//	@Override
//	public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
//		LOGGER.info("登录拦截--------");
//		String url = httpServletRequest.getRequestURI();
//		if (isExcludeUrl(url)) {
//			return true;
//		}
//		HttpSession session = httpServletRequest.getSession();
//		MasterInfoVo masterInfoVo = (MasterInfoVo) session.getAttribute(CommonUtils.CURRENT_MASTER);
//		Set<FunctionInfoVo> functionInfoVos = (Set<FunctionInfoVo>) session.getAttribute(CommonUtils.CURRENT_ROLE);
//
//		if (masterInfoVo == null) {
//			Cookie cookieTicket = getCookie(CommonUtils.COOKIE_TICKET);
//			if (cookieTicket != null) {
//				String ticket = cookieTicket.getValue();
//				if (StringUtils.isNotBlank(ticket)) {
//					LoginRecordQuery loginRecordQuery = new LoginRecordQuery();
//					loginRecordQuery.setTicket(ticket);
//					LoginRecordVo loginRecordVo = loginRecordService.select(loginRecordQuery);
//					if (loginRecordVo != null && isIpEqual(loginRecordVo.getIp())) {
//						masterInfoVo = masterInfoService.select(loginRecordVo.getMasterId());
//						if (masterInfoVo != null) {
//							LOGGER.info("获取用户成功--------");
//							session.setAttribute(CommonUtils.CURRENT_MASTER, masterInfoVo);
//							session.setAttribute(CommonUtils.CURRENT_MASTER_ID, masterInfoVo.getId());
//							functionInfoVos = functionInfoService.queryByMasterId(masterInfoVo.getId());
//							session.setAttribute(CommonUtils.CURRENT_ROLE, functionInfoVos);
//							request.setAttribute("currentUser", masterInfoVo);
//							request.setAttribute("functionInfos", functionInfoVos);
//							return true;
//						}
//					}
//				}
//			}
//			LOGGER.info("current url    " + url + "    current user null");
////			request.getRequestDispatcher(CommonUtils.LOGIN_URL).forward(request, response);
//			response.sendRedirect(CommonUtils.LOGIN_URL);
//			return false;
//		}
//		request.setAttribute("currentUser", masterInfoVo);
//		request.setAttribute("functionInfos", functionInfoVos);
//		return true;
//	}
//
//	private boolean isIpEqual(String ip) {
//		return HttpUtil.getRemoteHost(request).equals(ip);
//	}
//
//
//	private boolean isExcludeUrl(String url) {
//		if (excludeArray != null) {
//			for (String exclude : excludeArray) {
//				if (Pattern.matches(exclude, url)) {
//					return true;
//				}
//			}
//		}
//		return false;
//	}
//
//	private Cookie getCookie(String key) {
//		Cookie[] cookies = request.getCookies();
//		if (cookies != null) {
//			for (Cookie cookie : cookies) {
//				if (cookie.getName().equals(key)) {
//					return cookie;
//				}
//			}
//		}
//		return null;
//	}
//
//	private void addCookie(String key, String value, int time) {
//		int defalutTime = 60 * 60 * 24 * 7;
//		if (time > 0) {
//			defalutTime = time;
//		}
//		Cookie cookie = new Cookie(key, value);
//		cookie.setMaxAge(defalutTime);
//		cookie.setPath("/");
//		response.addCookie(cookie);
//	}
//
//
//	@Override
//	public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
//
//	}
//
//	@Override
//	public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
//
//	}
//
//	@Override
//	public void afterPropertiesSet() throws Exception {
//		excludeArray = excludeUrl.split(",");
//	}
//}
